package zmee.pmct.formplugin.pmas;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.utils.ObjectUtils;
import kd.bos.entity.filter.ControlFilters;
import kd.bos.form.events.AfterDoOperationEventArgs;
import kd.bos.ksql.util.StringUtil;
import kd.bos.list.plugin.AbstractListPlugin;
import kd.bos.logging.Log;
import kd.bos.logging.LogFactory;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.DispatchServiceHelper;
import kd.bos.util.HttpClientUtils;
import zmee.util.DBUtil;
import zmee.util.GetCwgxAccessTokenUtil;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class AfterPullEasYsdLP extends AbstractListPlugin {
    private static Log logger = LogFactory.getLog(AfterPullEasYsdLP.class);

    @Override
    public void afterDoOperation(AfterDoOperationEventArgs a) {
        super.afterDoOperation(a);
        String operateKey = a.getOperateKey();
        if (StringUtil.equals("pulleasysd",operateKey)){
            ControlFilters controlFilters = this.getControlFilters();
            List<Object> orgs = controlFilters.getFilter("org.id");
            ArrayList<String> orgnumbers = new ArrayList<>();
            ArrayList<Object> inputs = new ArrayList<>();
            if (!ObjectUtils.isEmpty(orgs)){
                for (Object org : orgs) {
                    DynamicObject dynamicObject = BusinessDataServiceHelper.loadSingle(org, "bos_org");
                    if (!ObjectUtils.isEmpty(dynamicObject)){
                        orgnumbers.add(dynamicObject.getString("zmee_easnumber"));
                    }
                }
            }
            inputs.add(orgnumbers);
            String accesstoken = GetCwgxAccessTokenUtil.getAccesstoken("18205188770");
            String cqUrl = DBUtil.getParamValue("CQUrl");
            String url = "/kapi/app/iscb/sf_T_MBG_BGDATA-zmee_project_budget";
//            String url = "/kapi/v2/iscb/route/sf_T_MBG_BGDATA-zmee_project_budget";
            url=cqUrl+url;
            HashMap<String, String> header = new HashMap<>();
            header.put("accesstoken",accesstoken);
            HashMap<String, Object> map = new HashMap<>();
            map.put("orgnumber",inputs);
            logger.info("AfterPullEasYsdLP参数："+map);
            try {
                String post = HttpClientUtils.postAppJson(url, header, map);

            } catch (IOException e) {
                e.printStackTrace();
            }

//            DispatchServiceHelper.invokeBizService("isc","iscb","IscFlowService",
//                    "start","T_MBG_BGDATA-zmee_project_budget",inputs);
        }

    }
}
